<template>
  <div :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts';
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '320px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartInfo: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el)
      this.setOptions(this.chartData)
    },
    setOptions({ xxData,yyData1,yyData2 } = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
          },
        },
        grid: {
          left: 20,
          right: 20,
          bottom: 10,
          top: 20,
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: this.chartInfo.xxData,
            axisLabel: {
              color:"rgb(233, 233, 233)"
            },
            axisTick: { //轴刻度线
              show: false
            },
          },
          {
            type: 'category',
            data: this.chartInfo.xxData,
            axisLine: {
              show: false
            },
            axisTick: {
              show: false
            },
            axisLabel: {
              show: false
            },
            splitArea: {
              show: false
            },
            splitLine: {
              show: false
            },
          }
        ],
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: '{value}',
            color:"rgb(202, 202, 202)"
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed',
              color:'rgb(88,88,88)'
            }
          }
        },
        series: [
          {
            data: this.chartInfo.yyData1,
            type: 'bar',
            barWidth:'20',
            itemStyle: {
              color: 'rgba(180, 180, 180, 0)'
            },
            showBackground: true,
            tooltip: {
              show: false
            },
            backgroundStyle: {
              color: 'rgba(180, 180, 180, 0.1)'
            }
          },
          {
            name: '',
            type: 'effectScatter',
            itemStyle: {
              color: function (arg) {
                const dayNum = arg.data[1]
                if (dayNum < 25) {
                  return 'rgb(243,66,65)'
                }else{
                  return 'rgb(40,200,246)'
                }
              }
            },
            symbolSize: 6,
            data: this.chartInfo.yyData2,
          },
          {
            data: this.chartInfo.yyData1,
            tooltip: {
              show: false
            },
            type: 'bar',
            barWidth:'2',
            xAxisIndex: 1,
            itemStyle: {
              color: function (arg) {
                const dayNum = arg.data
                if (dayNum < 25) {
                  return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: 'rgba(243,66,65,0.9)' },
                    { offset: 0.5, color: 'rgba(243,66,65,0.7)' },
                    { offset: 1, color: 'rgba(243,66,65,0)' }
                  ])
                }else{
                  return new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                    { offset: 0, color: 'rgba(40,200,246,0.9)' },
                    { offset: 0.5, color: 'rgba(40,200,246,0.7)' },
                    { offset: 1, color: 'rgba(40,200,246,0)' }
                  ])
                }
              }
            },
          }
        ]
      })
    }
  }
}
</script>
